package com.itcast.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcast.reggie.common.BaseContext;
import com.itcast.reggie.common.R;
import com.itcast.reggie.entity.ShoppingCart;
import com.itcast.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hehehe
 * @ClassName ShoppingCartController
 * @Description TODO
 * @date 2022/12/4/004 20:33
 * @Version 1.0
 */

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    private R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info(shoppingCart.toString());
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //if和else太多，废弃
      /*  LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        if (list != null) {
            if (list.size() == 1) {
                ShoppingCart shoppingCart1 = list.get(0);
                if (shoppingCart1.getDishFlavor() != null && !shoppingCart1.getDishFlavor().equals(shoppingCart.getDishFlavor())) {
                    shoppingCartService.save(shoppingCart);
                } else if (shoppingCart1.getDishFlavor() == null) {
                    shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
                    shoppingCartService.updateById(shoppingCart1);
                }
            } else {
                boolean flag = true;
                for (ShoppingCart cart : list) {
                    if (cart.getDishFlavor().equals(shoppingCart.getDishFlavor())) {
                        flag = false;
                        cart.setNumber(cart.getNumber() + 1);
                        shoppingCartService.updateById(cart);
                    }
                }
                if (flag) {
                    shoppingCartService.save(shoppingCart);
                }
            }
        } else {
            shoppingCartService.save(shoppingCart);
        }*/

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() != null) {
            queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
            queryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else {
            queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart != null) {
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        } else {
            shoppingCartService.save(shoppingCart);
            shoppingCart.setNumber(1);
            return R.success(shoppingCart);
        }
    }

    @PostMapping("/sub")
    private R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info(shoppingCart.toString());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() != null) {
            queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
            queryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else {
            queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
        if (cart == null) {
            return R.error("购物车中不存在此菜品或套餐！");
        } else if (cart.getNumber() >= 2) {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        } else {
            shoppingCartService.removeById(cart);
            cart.setNumber(0);
            return R.success(cart);
        }
    }

    @GetMapping("/list")
    private R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    private R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车已清空");
    }

}
